package com.e7.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class ValidateInterceptor implements Interceptor {

	private static final long serialVersionUID = 4776491269945054889L;

	private boolean isEmpty(String...args){
		for(String arg:args){
			if(arg==null||arg.trim().equals("")){
				return true;
			}
		}
		return false;
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();
		String value = request.getParameter("user.name");
		invocation.getProxy().getMethod();
		invocation.getStack().set("code", "-1");
		invocation.getStack().set("msg", "empty");
		if(this.isEmpty(value)){
			return "failed";
		}
		return invocation.invoke();
	}

}
